package com.licq.tv.data;

import com.licq.tv.ResourceTable;

import com.licq.tv.bean.JokeBean;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.RecycleItemProvider;
import ohos.agp.components.Text;

import java.util.ArrayList;

public class ListItemProvider extends RecycleItemProvider {
    private ArrayList<JokeBean> data = new ArrayList<>();
    private AbilitySlice mSlice;

    ListItemProvider(AbilitySlice abilitySlice) {
        mSlice = abilitySlice;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public Component getComponent(int position, Component convertView, ComponentContainer parent) {
        Component component = LayoutScatter.getInstance(mSlice).parse(ResourceTable.Layout_list_item, null, false);
        if (!(component instanceof ComponentContainer)) {
            return null;
        }
        component.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent secondIntent = new Intent();
//        // 指定待启动FA  的bundleName和abilityName
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.licq.tv")
                        .withAbilityName("com.licq.tv.VideoplayerAbility")
                        .build();
                secondIntent.setOperation(operation);
                mSlice.startAbility(secondIntent); // 通过AbilitySlice的startAbility接口实现启动另一个页面
            }
        });
        ComponentContainer rootLayout = (ComponentContainer) component;
        Text leftText = (Text) rootLayout.findComponentById(ResourceTable.Id_left_content);
        leftText.setText(String.valueOf(position));
        Text rightText = (Text) rootLayout.findComponentById(ResourceTable.Id_right_content);
        rightText.setText(data.get(position).getText());
        return component;
    }

    public void setData(ArrayList<JokeBean> arrayListBaseBean) {
        this.data = arrayListBaseBean;
        notifyDataChanged();

    }
}
